home *** CD-ROM | disk | FTP | other *** search
-
- /*© Copyright 1988-1992 UserLand Software, Inc. All Rights Reserved.*/
-
-
- #include "appletstrings.h"
- #include "appletmemory.h"
-
-
- void moveleft (psource, pdest, length) void *psource, *pdest; long length; {
-
- /*
- do a mass memory move with the left edge leading. good for closing
- up a gap in a buffer, among other things…
- */
-
- register char *ps, *pd;
- register long ctloops;
-
- ctloops = length;
-
- if (ctloops > 0) {
-
- ps = psource; /*copy into a register*/
-
- pd = pdest; /*copy into a register*/
-
- while (ctloops--) *pd++ = *ps++;
- }
- } /*moveleft*/
-
-
- void moveright (psource, pdest, length) void *psource, *pdest; long length; {
-
- /*
- do a mass memory move with the right edge leading. good for opening
- up a gap in a buffer, among other things…
- */
-
- register char *ps, *pd;
- register long ctloops;
-
- ctloops = length;
-
- if (ctloops > 0) {
-
- ps = (char *) psource + length - 1; /*right edge of source*/
-
- pd = (char *) pdest + length - 1; /*right edge of destination*/
-
- while (ctloops--) *pd-- = *ps--;
- }
- } /*moveright*/
-
-
- void fillchar (pfill, ctfill, chfill) void *pfill; long ctfill; char chfill; {
-
- /*
- do a mass memory fill -- copy ctfill chfills at pfill.
- */
-
- register char *p = pfill;
- register long ct = ctfill;
- register char ch = chfill;
-
- while (ct--) *p++ = (char) ch; /*tight loop*/
- } /*fillchar*/
-
-
- void clearbytes (pclear, ctclear) void *pclear; long ctclear; {
-
- /*
- fill memory with 0's.
- */
-
- fillchar (pclear, ctclear, (char) 0);
- } /*clearbytes*/
-
-
- void disposehandle (h) Handle h; {
-
- /*
- our own bottleneck for this built-in. we don't require type coercion
- and we check if its nil.
- */
-
- if (h != nil)
- DisposHandle (h);
- } /*disposehandle*/
-
-
- long gethandlesize (Handle h) {
-
- return (GetHandleSize (h));
- } /*gethandlesize*/
-
-
- boolean sethandlesize (Handle h, long size) {
-
- SetHandleSize (h, size);
-
- return (MemError () == noErr);
- } /*gethandlesize*/
-
-
- boolean copyhandle (horig, hcopy) Handle horig, *hcopy; {
-
- register Handle h;
- register long ct;
-
- if (horig == nil) { /*6/18/92 DW -- copying a nil handle begets a nil handle, returns true*/
-
- *hcopy = nil;
-
- return (true);
- }
-
- h = NewHandle (ct = gethandlesize (horig));
-
- if (h == nil)
- return (false);
-
- moveleft (*horig, *h, ct);
-
- *hcopy = h;
-
- return (true);
- } /*copyhandle*/
-
-
- boolean enlargehandle (Handle hgrow, long ctgrow, ptrchar newdata) {
-
- /*
- make the handle big enough to hold the new data, and move the new data in
- at the end of the newly enlarged handle.
- */
-
- register Handle h = hgrow;
- register long ct = ctgrow;
- register ptrchar p = newdata;
- register long origsize;
-
- origsize = gethandlesize (h);
-
- sethandlesize (h, origsize + ct);
-
- if (MemError () != noErr)
- return (false);
-
- moveleft (p, *h + origsize, ct);
-
- return (true);
- } /*enlargehandle*/
-
-
- boolean loadfromhandle (hload, ixload, ctload, pdata) Handle hload; long *ixload, ctload; ptrchar pdata; {
-
- /*
- copy the next ctload bytes from hload into pdata and increment the index.
-
- return false if there aren't enough bytes.
-
- start ixload at 0.
- */
-
- register Handle h = hload;
- register ptrchar p = pdata;
- register long ct = ctload;
- register long ix = *ixload;
- register long size;
-
- size = gethandlesize (h);
-
- if ((ix + ct) > size) /*asked for more bytes than there are*/
- return (false);
-
- moveleft (*h + ix, p, ct); /*copy out of the handle*/
-
- *ixload = ix + ct; /*increment the index into the handle*/
-
- return (true);
- } /*loadfromhandle*/
-
-
- boolean newtexthandle (bs, htext) bigstring bs; Handle *htext; {
-
- /*
- create a new handle to hold the text of the string.
-
- if the string is "\pABC" -- you get a handle of size 3.
- */
-
- register long len;
- register Handle h;
-
- h = NewHandle (len = stringlength (bs));
-
- if (h == nil)
- return (false);
-
- if (len > 0)
- moveleft (&bs [1], *h, len);
-
- *htext = h; /*pass handle back to caller*/
-
- return (true);
- } /*newtexthandle*/
-
-
- boolean pushtexthandle (bs, htext) bigstring bs; Handle htext; {
-
- /*
- htext is a handle created with newtexthandle.
-
- increase the size of the handle so we can push the text of bs at
- the end of the handle (not including length byte).
- */
-
- return (enlargehandle (htext, (long) stringlength (bs), (ptrchar) bs + 1));
- } /*pushtexthandle*/
-
-
- boolean pushhandleonhandle (Handle hsource, Handle hdest) {
-
- boolean fl;
-
- if (hsource == nil)
- return (true);
-
- lockhandle (hsource);
-
- fl = enlargehandle (hdest, gethandlesize (hsource), *hsource);
-
- unlockhandle (hsource);
-
- return (fl);
- } /*pushtexthandle*/
-
-
- void texthandletostring (Handle htext, bigstring bs) {
-
- long len;
-
- if (htext == nil) {
-
- setstringlength (bs, 0);
-
- return;
- }
-
- len = gethandlesize (htext);
-
- if (len > lenbigstring)
- len = lenbigstring;
-
- setstringlength (bs, len);
-
- moveleft (*htext, &bs [1], len);
- } /*texthandletostring*/
-
-
- void lockhandle (h) Handle h; {
-
- if (h != nil)
- HLock (h);
- } /*lockhandle*/
-
-
- void unlockhandle (h) Handle h; {
-
- if (h != nil)
- HUnlock (h);
- } /*unlockhandle*/
-
-
- boolean newclearhandle (ctbytes, hreturned) long ctbytes; Handle *hreturned; {
-
- register long ct = ctbytes;
- register Handle h;
-
- *hreturned = h = NewHandle (ct);
-
- if (h == nil)
- return (false);
-
- clearbytes (*h, ct);
-
- *hreturned = h;
-
- return (true);
- } /*newclearhandle*/
-
-
- boolean newfilledhandle (ptrvoid pdata, long size, Handle *hreturned) {
-
- register Handle h;
- register long ctbytes;
-
- ctbytes = size;
-
- h = NewHandle (ctbytes);
-
- if (h == nil) {
-
- *hreturned = nil;
-
- return (false);
- }
-
- moveleft (pdata, *h, ctbytes);
-
- *hreturned = h;
-
- return (true);
- } /*newfilledhandle*/
-
-
- boolean newheapstring (bigstring bs, hdlstring *hstring) {
-
- return (newfilledhandle (bs, (long) stringlength (bs) + 1, (Handle *) hstring));
- } /*newheapstring*/
-
-
- void copyheapstring (hdlstring hstring, bigstring bs) {
-
- if (hstring == nil) {
-
- setstringlength (bs, 0);
-
- return;
- }
-
- lockhandle ((Handle) hstring);
-
- copystring (*hstring, bs);
-
- unlockhandle ((Handle) hstring);
- } /*copyheapstring*/
-
-
- boolean sethandlecontents (void *p, long ct, Handle h) {
-
- sethandlesize (h, ct);
-
- if (MemError () != noErr)
- return (false);
-
- moveleft (p, *h, ct);
-
- return (true);
- } /*sethandlecontents*/
-
-
- boolean minhandlesize (Handle h, long size) {
-
- if (gethandlesize (h) >= size) /*already big enough*/
- return (true);
-
- sethandlesize (h, size); /*try to make it larger*/
-
- return (MemError () == noErr);
- } /*minhandlesize*/
-
-
- boolean newintarray (short ct, hdlintarray *harray) {
-
- Handle h;
-
- if (!newclearhandle (longsizeof (short) * ct, &h))
- return (false);
-
- *harray = (hdlintarray) h;
-
- return (true);
- } /*newintarray*/
-
-
- boolean setintarray (hdlintarray harray, short ix, short val) {
-
- /*
- assign into a cell in a variable-size array of integers, 0-based index.
-
- return false if the array needed to be extended but there isn't enough
- memory to do it.
- */
-
- register hdlintarray h = harray;
-
- if (!minhandlesize ((Handle) h, (ix + 1) * longsizeof (short)))
- return (false);
-
- (*harray) [ix] = val;
-
- return (true);
- } /*setintarray*/
-
-
- boolean getintarray (hdlintarray harray, short ix, short *val) {
-
- *val = (*harray) [ix];
-
- return (true);
- } /*getintarray*/
-
-
- void fillintarray (hdlintarray harray, short val) {
-
- register hdlintarray h = harray;
- register short x = val;
- register short ct;
-
- ct = gethandlesize ((Handle) h) / longsizeof (short);
-
- while (--ct >= 0)
- (*h) [ct] = x;
- } /*fillintarray*/
-
-
-